package customerEnter.Frame.redis.Major;

import java.util.concurrent.TimeUnit;

import net.sf.json.JSONArray;

import org.springframework.data.redis.serializer.GenericToStringSerializer;

import com.alibaba.fastjson.JSON;

import customerEnter.Frame.DaoDataTransmission.PagingParametersResult;
import customerEnter.tool.WhetherToEmpty;

/**
 * redis map 緩存處理
 * 
 * 类名称：RedisClientOpenHashMap 类描述： 创建人：dy 创建时间：2016-9-7 上午10:27:29
 * 
 */
public class RedisClientOpsForValue {
	/**
	 * 設置值
	 */
	public static void setForValue(String key, String value) {
		RedisClient.getRedisTemplate().opsForValue().set(key, value);
	}

	/**
	 * 取得jsonarray 數據
	 */
	public static JSONArray getForVlaue(String key) {
		String str = RedisClient.getRedisTemplate().opsForValue().get(key);
		if (WhetherToEmpty.StringWhetherToEmpty(str)) {
			return null;
		} else {
			JSONArray jsonArray = new JSONArray();
			jsonArray = JSONArray.fromObject(str);
			return jsonArray;
		}
	}

	@SuppressWarnings("rawtypes")
	public static PagingParametersResult getForValueObject(String key) {
		String str = RedisClient.getRedisTemplate().opsForValue().get(key);
		if (WhetherToEmpty.StringWhetherToEmpty(str)) {
			return null;
		} else {
			return JSON.parseObject(str, PagingParametersResult.class);
		}
	}

	/**
	 * 判讀key 是否存在
	 */
	public static boolean IfKeyExist(String key) {
		long long1 = RedisClient.getRedisTemplate().opsForValue().size(key);
		if (long1 <= 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 設置某一個key 的過期時間
	 */
	public static void setOverdueTime(String key, int timeout, TimeUnit unit) {
		RedisClient.getRedisTemplate().expire(key, timeout, unit);
	}

	/**
	 * 取值的時候 序列化String
	 */
	public void ValueStringSerializer() {
		RedisClient.getRedisTemplate().setValueSerializer(new GenericToStringSerializer<String>(String.class));
	}

	/**
	 * 取值的時候 序列化Long
	 */
	public void ValueLongSerializer() {
		RedisClient.getRedisTemplate().setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
	}

	/**
	 * 取值的時候 序列化 Object
	 */
	public void ValueObjectSerializer() {
		RedisClient.getRedisTemplate().setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
	}

}
